﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Admin
{
    public partial class adminCreateNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorCreateNewsText.Width = 450;
                ckeditorCreateNewsLocalizedText.Width = 450;

                this.Form.DefaultButton = btnAdminCreateNews.UniqueID;

                BindSubMenu();
                lblAdminCreateNewsSucces.Visible = false;

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkAdminDashboard.NavigateUrl = "~/Admin/admin.aspx";
            hyLinkUserHandling.NavigateUrl = "~/Admin/adminUsers.aspx";
            hyLinkAdminExchange.NavigateUrl = "~/Admin/adminExchange.aspx";
            hyLinkNewsHandling.NavigateUrl = "~/Admin/adminNews.aspx";
            hyLinkEditInformation.NavigateUrl = "~/Admin/adminInformation.aspx";
            hyLinkContactPerson.NavigateUrl = "~/Admin/adminContactPerson.aspx";
            hyLinkResetUsers.NavigateUrl = "~/Admin/adminUsersReset.aspx";
            hylinkDepartment.NavigateUrl = "~/Admin/adminDepartment.aspx";
            hyLinkAdminCreateNews.NavigateUrl = "~/Admin/adminNewsCreate.aspx";
        }

        /// <summary>
        /// Creates a news article, based on the input parameters.
        /// </summary>
        /// <param name="title">Specifies the title to be used.</param>
        /// <param name="text">Specifies the text to be used.</param>
        /// <param name="hasImage">Specifies whether or not news article has an image.</param>
        /// <param name="imageFileName">Specifies the file name of the image to be used.</param>
        /// <param name="culture">Specifies the culture to be used in the localized news article.</param>
        /// <param name="locTitle">Specifies the title to be used in the localized news article.</param>
        /// <param name="locText">Specifies the text to be used in the localized news article.</param>
        private void CreateNews(string title, string text, bool hasImage, string imageFileName, string culture, string locTitle, string locText)
        {
            dbAdmin.CreateNews(title, text, hasImage, imageFileName, culture, locTitle, locText);
        }

        /// <summary>
        /// Uploads an image.
        /// </summary>
        private void UploadPicture() 
        {
            string path = Server.MapPath("~/Images/News/" + txtCreateNewsTitle.Text.Trim() + "_" + DateTime.Now.ToShortDateString() + "/");
            if (!Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);

                dir.Create();
            }

            if (fuCreateNewsImage.HasFile)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(fuCreateNewsImage.FileContent);
                string imagePath = path + fuCreateNewsImage.FileName;
                string thumbPath = path + "thumb_" + fuCreateNewsImage.FileName;

                GeneralTools.ResizeImage(350, image, imagePath);
                GeneralTools.CreateThumbnail(100, image, thumbPath);
            }
        }

        protected void btnAdminCreateNews_Click(object sender, EventArgs e)
        {
            string title = txtCreateNewsTitle.Text;
            string text = ckeditorCreateNewsText.Text;
            bool hasImage = fuCreateNewsImage.HasFile;
            string imageFileName = fuCreateNewsImage.FileName;
            string locTitle = txtCreateNewsLocalizedTitle.Text;
            string locText = ckeditorCreateNewsLocalizedText.Text;
            string culture = "en-GB";

            //if (HttpContext.Current.Session["MyCulture"] != null)
            //    culture = HttpContext.Current.Session["MyCulture"].ToString();

            UploadPicture();
            CreateNews(title, text, hasImage, imageFileName, culture, locTitle, locText);
            panAdminCreateNews.Visible = false;
            lblAdminCreateNewsSucces.Visible = true;
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            hyLinkAdminDashboard.Text = GetLocalResourceObject("hyLinkAdminDashboardText").ToString();
            hyLinkUserHandling.Text = GetLocalResourceObject("hyLinkUserHandlingText").ToString();
            hyLinkAdminExchange.Text = GetLocalResourceObject("hyLinkAdminExchangeText").ToString();
            hyLinkNewsHandling.Text = GetLocalResourceObject("hyLinkNewsHandlingText").ToString();
            hyLinkEditInformation.Text = GetLocalResourceObject("hyLinkEditInformationText").ToString();
            hyLinkResetUsers.Text = GetLocalResourceObject("hyLinkResetUsersText").ToString();
            hyLinkContactPerson.Text = GetLocalResourceObject("hyLinkContactPersonText").ToString();
            hylinkDepartment.Text = GetLocalResourceObject("hylinkDepartmentText").ToString();
            hyLinkAdminCreateNews.Text = GetLocalResourceObject("hyLinkAdminCreateNewsText").ToString();

            lblAdminContentHeader.Text = GetLocalResourceObject("lblAdminContentHeaderText").ToString();
            lblAdminCreateNewsSucces.Text = GetLocalResourceObject("lblAdminCreateNewsSuccesText").ToString();
            lblCreateNewsImage.Text = GetLocalResourceObject("lblCreateNewsImageText").ToString();
            lblCreateNewsText.Text = GetLocalResourceObject("lblCreateNewsTextText").ToString();
            lblCreateNewsTitle.Text = GetLocalResourceObject("lblCreateNewsTitleText").ToString();
            lblCreateNewsLocalizedTitle.Text = GetLocalResourceObject("lblCreateNewsLocalizedTitleText").ToString();
            lblCreateNewsLocalizedText.Text = GetLocalResourceObject("lblCreateNewsLocalizedTextText").ToString();
            lblLocalized.Text = GetLocalResourceObject("lblLocalizedText").ToString();

            btnAdminCreateNews.Text = GetLocalResourceObject("btnAdminCreateNewsText").ToString();

            string imageValidaton = GetLocalResourceObject("regexvalNewsImageText").ToString();
            regexvalNewsImage.ToolTip = imageValidaton;
            regexvalNewsImage.ErrorMessage = imageValidaton;

            string textValidation = GetLocalResourceObject("rfvalTextText").ToString();
            rfvalText.ToolTip = textValidation;
            rfvalText.ErrorMessage = textValidation;

            string titleValidation = GetLocalResourceObject("rfvalTitleText").ToString();
            rfvalTitle.ToolTip = titleValidation;
            rfvalTitle.ErrorMessage = titleValidation;

            string englishTitleValidation = GetLocalResourceObject("rfvalCreateNewsLocalizedTitleText").ToString();
            rfvalCreateNewsLocalizedTitle.ToolTip = englishTitleValidation;
            rfvalCreateNewsLocalizedTitle.ErrorMessage = englishTitleValidation;

            string englishTextValidation = GetLocalResourceObject("rfvalCreateNewsLocalizedTextText").ToString();
            rfvalCreateNewsLocalizedText.ToolTip = englishTextValidation;
            rfvalCreateNewsLocalizedText.ErrorMessage = englishTextValidation;

            valSumAdminCreateNews.HeaderText = GetLocalResourceObject("valSumAdminCreateNewsText").ToString();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
            {
                ckeditorCreateNewsLocalizedText.Language = "en";
                ckeditorCreateNewsText.Language = "en";
            }
            else 
            {
                ckeditorCreateNewsLocalizedText.Language = "da";
                ckeditorCreateNewsText.Language = "da";
            }
        }


    }
}